package com.jason.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jason.system.common.util.Pager;
import com.jason.system.entity.Number;
import com.jason.system.mapper.NumberMapper;
import com.jason.system.service.NumberService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author jie
 * @version 1.0
 * success come from self-discipline
 * @date 2021/3/25 17:14
 */
@Service
public class NumberServiceImpl implements NumberService {

  @Resource
  private NumberMapper numberMapper;

  @Override
  public Pager<Number> getAllNumbers(Integer page ,Integer size) {
    PageHelper.startPage(page,size);
    List<Number> numbers = numberMapper.getAllNumbers();
    PageInfo<Number> numberPageInfo = new PageInfo<>(numbers);
    return new Pager<>(numberPageInfo);
  }

  @Override
  public Pager<Number> searchNumbers(Integer page, Integer size, String address, String stuName) {
    PageHelper.startPage(page,size);
    List<Number> numbers = numberMapper.getSearchNumbers(address,stuName);
    PageInfo<Number> numberPageInfo = new PageInfo<>(numbers);
    return new Pager<>(numberPageInfo);
  }

  @Override
  public Pager<Number> getAllNumbersByTime(Integer page, Integer size, Date startTime, Date endTime) {
    PageHelper.startPage(page,size);
    List<Number> numbers = numberMapper.getAllNumbersByTime(startTime,endTime);
    PageInfo<Number> numberPageInfo = new PageInfo<>(numbers);
    return new Pager<>(numberPageInfo);
  }

  @Override
  public Number getNumber(String number) {
    if("".equals(number)||number==null){
      return null;
    }
    return numberMapper.getNumber(number);
  }

  @Override
  public void insertNumber(Number number) {
    numberMapper.insertNumber(number);
  }

  @Override
  public void updateNumber(Number number) {
    numberMapper.updateNumber(number);
  }

  @Override
  public boolean deleteNumber(Integer nId) {
    if (nId==null){
      return false;
    }else {
      numberMapper.deleteNumber(nId);
      return true;
    }
  }

  @Override
  public boolean deleteNumbers(String nIds) {
    if (nIds==null||"".equals(nIds)){
      return false;
    }else {
      List<Integer> nIdsList = Arrays.stream(nIds.split(",")).map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
      numberMapper.deleteNumbers(nIdsList);
      return true;
    }
  }
}
